/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.base.entity.BaBox;
import com.thinkgem.jeesite.modules.base.service.BaBoxService;

/**
 * 周转箱Controller
 * @author alex
 * @version 2017-05-04
 */
@Controller
@RequestMapping(value = "${adminPath}/base/baBox")
public class BaBoxController extends BaseController {

	@Autowired
	private BaBoxService baBoxService;
	
	@ModelAttribute
	public BaBox get(@RequestParam(required=false) String id) {
		BaBox entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baBoxService.get(id);
		}
		if (entity == null){
			entity = new BaBox();
		}
		return entity;
	}
	
	@RequiresPermissions("base:baBox:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaBox baBox, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BaBox> page = baBoxService.findPage(new Page<BaBox>(request, response), baBox); 
		model.addAttribute("page", page);
		return "modules/base/baBoxList";
	}

	@RequiresPermissions("base:baBox:view")
	@RequestMapping(value = "form")
	public String form(BaBox baBox, Model model) {
		model.addAttribute("baBox", baBox);
		return "modules/base/baBoxForm";
	}

	@RequiresPermissions("base:baBox:edit")
	@RequestMapping(value = "save")
	public String save(BaBox baBox, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baBox)){
			return form(baBox, model);
		}
		baBoxService.save(baBox);
		addMessage(redirectAttributes, "保存周转箱成功");
		return "redirect:"+Global.getAdminPath()+"/base/baBox/?repage";
	}
	
	@RequiresPermissions("base:baBox:edit")
	@RequestMapping(value = "delete")
	public String delete(BaBox baBox, RedirectAttributes redirectAttributes) {
		baBoxService.delete(baBox);
		addMessage(redirectAttributes, "删除周转箱成功");
		return "redirect:"+Global.getAdminPath()+"/base/baBox/?repage";
	}

}